package zhongbo.java.air.monitor.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.monitor.mapper.FlightActivityDailyMapper;
import zhongbo.java.air.monitor.model.FlightActivityDaily;
import zhongbo.java.air.monitor.service.IFlightActivityDailyService;

import java.util.Date;
import java.util.List;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月08日 星期二 04:07:41
 */
@Service
@Slf4j
public class FlightActivityDailyServiceImpl implements IFlightActivityDailyService {


    @Autowired
    private FlightActivityDailyMapper flightActivityDailyMapper;

    @Override
    public List<FlightActivityDaily> selectDailyStats(String date) {
        return flightActivityDailyMapper.selectDailyStats(date);
    }

    @Override
    public List<FlightActivityDaily> getDailyStats(String date) {
        if (StrUtil.isBlank(date)) {
            throw new RuntimeException("data参数必须填写!示例: 2025-04-08");
        }
        //根据 date 条件, 从 air_flight_activity_daily 表里查询结果,并返回
        QueryWrapper<FlightActivityDaily> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(date)) {
            queryWrapper.eq("date", date);
        }

        List<FlightActivityDaily> flightActivityDailies = flightActivityDailyMapper.selectList(queryWrapper);
        if (flightActivityDailies == null) {
            throw new RuntimeException("当天无活动飞机");
        }
        return flightActivityDailies;
    }

    @Override
    public FlightActivityDaily add(FlightActivityDaily activityDaily) {
        validFlight(activityDaily);
        //Date:2024-10-24T12:34:56.789Z
        //LocalDate.now():输出类似: 2024-10-24
        // 首次新增, 则设置创建时间和更新时间为当前时间
        // 后续修改, 则更新更新时间为当前时间
        activityDaily.setCreatedTime(new Date());
        activityDaily.setUpdatedTime(new Date());
        activityDaily.setDate(new Date());

        int insert = flightActivityDailyMapper.insert(activityDaily);
        if (insert > 0) {
            return activityDaily;
        }
        throw new RuntimeException("新增统计失败");
    }

    /**
     * 验证
     *
     * @param activityDaily 实体
     */
    private void validFlight(FlightActivityDaily activityDaily) {
        if (activityDaily == null) {
            throw new RuntimeException("信息不能为空");
        }
        String code = activityDaily.getIcaoCode();
        if (code.length() > 6) {
            throw new RuntimeException("ICAO不能不能超过6个字段");
        }
        // List<FlightActivityDaily> activityDailies = flightActivityDailyMapper.selectList(null);
        // for (FlightActivityDaily a : activityDailies) {
        //     if (Objects.equals(code, a.getIcaoCode())) {
        //         throw new RuntimeException("ICAO不能重复");
        //     }
        // }

    }

    @Override
    public String delete(String id) {
        if (StrUtil.isBlank(id)) {
            throw new RuntimeException("id不能为空");
        }
        int delete = flightActivityDailyMapper.deleteById(id);
        if (delete > 0) {
            return id;
        }
        throw new RuntimeException("删除统计失败");

    }

    @Override
    public FlightActivityDaily edit(FlightActivityDaily activityDaily1) {
        validFlight(activityDaily1);
        String id = activityDaily1.getId();
        if (StrUtil.isBlank(id)) {
            throw new RuntimeException("id不能为空");
        }
        findOne(id);
        // todo 设置完修改的数据后， 再调用mapper 的updateById方法（所有地方都要改）
        activityDaily1.setUpdatedTime(new Date());
        activityDaily1.setDate(new Date());
        int update = flightActivityDailyMapper.updateById(activityDaily1);
        if (update > 0) {
            return activityDaily1;
        }
        throw new RuntimeException("修改此条统计失败");
    }

    @Override
    public FlightActivityDaily findOne(String id) {
        if (StrUtil.isBlank(id)) {
            throw new RuntimeException("id不能为空");
        }
        FlightActivityDaily activityDaily = flightActivityDailyMapper.selectById(id);
        if (activityDaily == null) {
            throw new RuntimeException("此条统计不存在");
        } else {
            return activityDaily;
        }

    }

    @Override
    public PageVo<FlightActivityDaily> findPage(String keyword, String fields,
                                                String icao, String startDate, String endDate,
                                                Integer pageNumber, Integer pageSize, String oderByField, Boolean oderByAsc) {
        QueryWrapper<FlightActivityDaily> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(icao)) {
            queryWrapper.eq("icao_code", icao);
        }
        if (StrUtil.isNotBlank(startDate)) {
            queryWrapper.ge("date", startDate);
        }
        if (StrUtil.isNotBlank(endDate)) {
            queryWrapper.le("date", endDate);
        }
        //支持指定字段模糊搜索Keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.like(StrUtil.toUnderlineCase(field), keyword);
            }

        }
        //检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            //检测室正序还是倒序
            boolean isOderByAsc = Boolean.TRUE.equals(oderByAsc);
            if (isOderByAsc) {
                //正
                queryWrapper.orderByAsc(oderByField);
            } else {
                //倒序
                queryWrapper.orderByAsc(oderByField);
            }
        }
        Page<FlightActivityDaily> pageParam = new Page<>(pageNumber, pageSize);
        Page<FlightActivityDaily> flightPage = flightActivityDailyMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(flightPage);
    }


}
